library(haven)
library(tidyverse)
library(mgcv)
library(tidygam)

data_17 <- read_xpt('LLCP2017.XPT')

view(colnames(data_17))
table(data_17$`_AGE80`)
table(data_17$SEX)

mean(data_17$TRNSGNDR > 5, na.rm = T)
sum(data_17$TRNSGNDR > 5, na.rm = T)
sum(is.na(data_17$TRNSGNDR))

df.17 <- data_17 %>%
  filter(TRNSGNDR < 5)
df.17$age <- df.17$'_AGE80'

mean(df.17$age == 80, na.rm = T)
sum(df.17$age == 80, na.rm = T)

df.17 <- df.17 %>%
  filter(age < 80) %>%
  mutate(trans = if_else(TRNSGNDR < 4, 1, 0),
         transfem = if_else(TRNSGNDR == 1, 1, 0),
         transmasc = if_else(TRNSGNDR == 2, 1, 0),
         gnc = if_else(TRNSGNDR == 3, 1, 0),
         gender_id = case_when(TRNSGNDR == 1 ~ "Transfeminine",
                               TRNSGNDR == 2 ~ "Transmasculine",
                               TRNSGNDR == 3 ~ "Gender nonconforming",
                               TRNSGNDR == 4 & SEX == 1 ~ "Cis man",
                               TRNSGNDR == 4 & SEX == 2 ~ "Cis woman"))

#Age distributions

df.17 %>% #all participants
  ggplot() +
  aes(x = age) +
  geom_histogram(binwidth = 2, col = "deepskyblue4", fill = "deepskyblue3")

df.17 %>% #trans participants
  filter(trans == 1) %>%
  ggplot() +
  aes(x = age) +
  geom_histogram(binwidth = 2, col = "deepskyblue4", fill = "deepskyblue3")

#Clean covariates
df.17$race2 <- df.17$`_MRACE1`
df.17$ethnicity2 <- df.17$`_HISPANC`
df.17 <- df.17 %>%
  mutate(ethnicity = case_when(ethnicity2 == 1 ~ "Hispanic",
                               ethnicity2 == 2 ~ "Non-Hispanic"),
         
         race = case_when(race2 == 1 ~ "White",
                          race2 == 2 ~ "Black",
                          race2 %in% c(3,5,6) ~ "Other",
                          race2 == 4 ~ "Asian",
                          race2 == 7 ~ "Multiracial"))

df.17 <- df.17 %>%
  mutate(edu = case_when(EDUCA < 4 ~ "Less than HS",
                         EDUCA < 5 ~ "HS",
                         EDUCA < 6 ~ "Some college",
                         EDUCA == 6 ~ "College degree"))


df.17 <- df.17 %>%
  mutate(edu = case_when(EDUCA < 4 ~ "Less than HS",
                         EDUCA < 5 ~ "HS",
                         EDUCA < 6 ~ "Some college",
                         EDUCA == 6 ~ "College degree"))

df.17 <- df.17 %>%
  mutate(diabetes = case_when(DIABETE3 == 1 ~ 1,
                              DIABETE3 %in% c(2:4) ~ 0),
         diabetes_early = case_when(DIABAGE2 < 25 ~ 1,
                                    DIABAGE2 %in% c(26:80) ~ 0),
         depression = case_when(ADDEPEV2 == 1 ~ 1,
                                ADDEPEV2 == 2 ~ 0),
         arthritis = case_when(HAVARTH3 == 1 ~ 1,
                               HAVARTH3 == 2 ~ 0))

#Clean health outcomes
df.17 <- df.17 %>%
  mutate(fair_poor = case_when(GENHLTH < 4 ~ 0,
                               GENHLTH < 6 ~ 1))

df.17$trans_fac <- factor(df.17$trans,
                          levels = c(0,1),
                          labels = c("Cis", "Trans"))

df.17$weights <- df.17$'_LLCPWT'

df.17$id <- df.17$'_PSU'

df.17$state_id <- df.17$'_STATE'

df.17$race1 <- df.17$`_RACE1`

df.17$blind <- case_when(df.17$BLIND == 2 ~ 0,
                         df.17$BLIND == 1 ~ 1)
df.17$deaf <- case_when(df.17$DEAF == 2 ~ 0,
                        df.17$DEAF == 1 ~ 1)
df.17$decide <- case_when(df.17$DECIDE == 2 ~ 0,
                          df.17$DECIDE == 1 ~ 1)
df.17$walk <- case_when(df.17$DIFFWALK == 2 ~ 0,
                        df.17$DIFFWALK == 1 ~ 1)
df.17$dress <- case_when(df.17$DIFFDRES == 2 ~ 0,
                         df.17$DIFFDRES == 1 ~ 1)
df.17$alone <- case_when(df.17$DIFFALON == 2 ~ 0,
                         df.17$DIFFALON == 1 ~ 1)

df.17$disabled <- if_else(df.17$blind + df.17$deaf + df.17$decide + df.17$walk + df.17$dress + df.17$alone > 0, 1, 0)

#view(colnames(df.22))

clean_17 <- df.17 %>%
  select(SEX, HTIN4, age:disabled)

clean_17 <- clean_17 %>%
  mutate(year = 2017)

write_csv(clean_17, "Clean data 2017.csv")
